package com.wjh.www.service.impl;

import com.wjh.www.annotation.spring.Autowired;
import com.wjh.www.annotation.spring.Component;
import com.wjh.www.dao.mapper.ClazzMapper;
import com.wjh.www.dao.mapper.CollegeMapper;
import com.wjh.www.dao.mapper.UserMapper;
import com.wjh.www.entity.dto.CommonResult;
import com.wjh.www.entity.po.Clazz;
import com.wjh.www.entity.po.College;
import com.wjh.www.entity.po.User;
import com.wjh.www.entity.vo.ClazzInfoVo;
import com.wjh.www.entity.vo.UserInfoVo;
import com.wjh.www.service.ClazzService;
import com.wjh.www.service.constant.SystemConstant;

import java.util.ArrayList;
import java.util.List;

/**
 * 班级相关业务
 *
 * @author wjh
 * @date 2021/9/10 16:16
 * @Package com.wjh.anyview.service.impl
 */
@Component
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CollegeMapper collegeMapper;

    @Override
    public CommonResult<?> addClazz(Clazz clazz) {
        if (clazzMapper.selectOneByFields(clazz) != null) {
            return CommonResult.operateFailWithMessage("该班级已经存在本系统！");
        }
        if (clazzMapper.insert(clazz) > 0) {
            return CommonResult.operateSuccess();
        }
        return CommonResult.operateFailWithMessage(SystemConstant.SYSTEM_ERROR);
    }

    @Override
    public CommonResult<?> deleteClazzById(Long clazzId) {
        Clazz clazzDelete = new Clazz(clazzId);
        if (clazzMapper.delete(clazzDelete) > 0) {
            return CommonResult.operateSuccess();
        }
        return CommonResult.operateFailWithMessage(SystemConstant.SYSTEM_ERROR);
    }

    @Override
    public CommonResult<List<UserInfoVo>> queryClazzById(Long clazzId) {
        User userSelect = new User();
        userSelect.setClassId(clazzId);
        List<User> users = userMapper.selectListByFields(userSelect);
        System.out.println(users);
        if (users.size() == 0) {
            return CommonResult.operateFailWithMessage("该班级没有学生或班级代码错误！");
        }
        List<UserInfoVo> list = new ArrayList<>();
        for (User user : users) {
            UserInfoVo userInfoVo = new UserInfoVo();
            userInfoVo.setNum(user.getNum());
            userInfoVo.setName(user.getName());
            userInfoVo.setTelephone(user.getTelephone());
            userInfoVo.setId(user.getId());
            userInfoVo.setSex(user.getSex());
            Clazz clazz = clazzMapper.selectById(new Clazz(user.getClassId()));
            userInfoVo.setClassName(clazz.getName());
            userInfoVo.setGrade(user.getGrade());
            list.add(userInfoVo);
        }
        return CommonResult.operateSuccess(list);
    }

    @Override
    public CommonResult<?> studentChangeClazz(Long clazzId, Long studentId) {
        User user = new User(studentId);
        user.setClassId(clazzId);
        if (userMapper.updateById(user) > 0) {
            return CommonResult.operateSuccess();
        }
        return CommonResult.operateFailWithMessage(SystemConstant.SYSTEM_ERROR);
    }

    @Override
    public CommonResult<?> deleteStudentInClazz(Long studentId) {
        return studentChangeClazz(0L, studentId);
    }

    @Override
    public CommonResult<List<ClazzInfoVo>> queryAllClazz() {
        List<Clazz> clazzes = clazzMapper.selectListByFields(new Clazz());
        List<ClazzInfoVo> list = new ArrayList<>();
        for (Clazz clazz : clazzes) {
            College college = collegeMapper.selectById(new College(clazz.getCollegeId()));
            ClazzInfoVo clazzInfoVo = new ClazzInfoVo();
            clazzInfoVo.setId(clazz.getId());
            clazzInfoVo.setName(clazz.getName());
            clazzInfoVo.setGrade(clazz.getGrade());
            clazzInfoVo.setCollegeName(college.getName());
            list.add(clazzInfoVo);
        }
        return CommonResult.operateSuccess(list);
    }
}
